
package states;

import java.util.logging.Level;
import java.util.logging.Logger;
import management.BotoMenu;
import management.DBAccess;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;


public class GameOverState extends BasicGameState {

    DBAccess dba = new DBAccess();
    public static final int ID = 5;
    // Contenidor del joc
    private GameContainer container;
    private StateBasedGame state;
    // Imatge fons de pantalla
    private Image imgFons;
    private Image imgSortir;
    private Image imgSortirSelected;
    private Image imgCarregar;
    private Image imgCarregarSelected;
    private BotoMenu btnSortir;
    private BotoMenu btnCarregar;

    @Override
    public int getID() {
        return ID;
    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        this.container = gc;
        this.state = sbg;
        imgFons = dba.recuperarMenuResource("fonsGameOver");
        imgSortir = dba.recuperarMenuResource("btnSortirBlack");
        imgSortirSelected = dba.recuperarMenuResource("btnSortirSelectedGO");
        imgCarregar = dba.recuperarMenuResource("btnCarregarBlack");
        imgCarregarSelected = dba.recuperarMenuResource("botoCarregarOver");
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics grphcs) throws SlickException {
        imgFons.draw(0, 0);
        btnSortir.render(gc, grphcs);
        btnCarregar.render(gc, grphcs);
    }


    private void crearBotonsMenuGameOver() {
        btnSortir = new BotoMenu(container, imgSortir, 300, 350);
        btnSortir.setMouseOverImage(imgSortirSelected);
        btnCarregar = new BotoMenu(container, imgCarregar, 300, 300);
        btnCarregar.setMouseOverImage(imgCarregarSelected);
    }

    private void afegirListeners() {
        btnCarregar.addListener(new ComponentListener() {

            public void componentActivated(AbstractComponent ac) {
                try {
                    state.init(container);
                    state.enterState(LoadGameState.ID);
                } catch (SlickException ex) {
                    Logger.getLogger(GameOverState.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        btnSortir.addListener(new ComponentListener() {

            public void componentActivated(AbstractComponent source) {
                try {
                    state.init(container);
                    state.enterState(MainMenuState.ID);
                } catch (SlickException ex) {
                    Logger.getLogger(GameOverState.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

    }

    public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException {
        btnCarregar.setActiu(true);
        btnSortir.setActiu(true);
    }


     @Override
    public void enter(GameContainer gc, StateBasedGame state) {
        crearBotonsMenuGameOver();
        afegirListeners();
    }

   }

